package com.lrkj.payfee.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.payfee.domain.Refill;
import com.lrkj.payfee.service.IRefillService;
import com.lrkj.property.domain.Room;
import com.lrkj.property.service.IRoomService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.RefillCardMapper;
import com.lrkj.payfee.domain.RefillCard;
import com.lrkj.payfee.service.IRefillCardService;

/**
 * 充值卡Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-27
 */
@Service
public class RefillCardServiceImpl implements IRefillCardService
{
    @Autowired
    private RefillCardMapper refillCardMapper;

    @Autowired
    private IRefillService refillService;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private IRoomService roomService;

    /**
     * 查询充值卡
     *
     * @param cardId 充值卡ID
     * @return 充值卡
     */
    @Override
    public RefillCard selectRefillCardById(String cardId)
    {
        return refillCardMapper.selectRefillCardById(cardId);
    }

    /**
     * 查询充值卡列表
     *
     * @param refillCard 充值卡
     * @return 充值卡
     */
    @Override
    public List<RefillCard> selectRefillCardList(RefillCard refillCard)
    {
        return refillCardMapper.selectRefillCardList(refillCard);
    }

    /**
     * 新增充值卡
     *
     * @param refillCard 充值卡
     * @return 结果
     */
    @Override
    public int insertRefillCard(RefillCard refillCard)
    {
        refillCard.setCardId(IdUtils.simpleUUID());
        refillCard.setCreateBy(SecurityUtils.getUsername());
        refillCard.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(refillCard);
        return refillCardMapper.insertRefillCard(refillCard);
    }

    /**
     * 修改充值卡
     *
     * @param refillCard 充值卡
     * @return 结果
     */
    @Override
    public int updateRefillCard(RefillCard refillCard)
    {
        this.setUnitInfo(refillCard);
        refillCard.setUpdateBy(SecurityUtils.getUsername());
        refillCard.setUpdateTime(DateUtils.getNowDate());
        return refillCardMapper.updateRefillCard(refillCard);
    }

    /**
     * 批量删除充值卡
     *
     * @param cardIds 需要删除的充值卡ID
     * @return 结果
     */
    @Override
    public int deleteRefillCardByIds(String[] cardIds)
    {
        orgTreeService.deleteOrgTreeByIds(cardIds);
        return refillCardMapper.deleteRefillCardByIds(cardIds);
    }

    /**
     * 删除充值卡信息
     *
     * @param cardId 充值卡ID
     * @return 结果
     */
    @Override
    public int deleteRefillCardById(String cardId)
    {
        orgTreeService.deleteOrgTreeById(cardId);
        return refillCardMapper.deleteRefillCardById(cardId);
    }


    private void setUnitInfo(RefillCard bean) {
        Room room= roomService.selectRoomById(bean.getRoomId());
        bean.setAncestors(room.getAncestors() + "," + room.getId());
        bean.setDeptId(room.getDeptId());
        //orgTreeService.insertOrgTree(bean.getCardId(), parentBean.getRefillName(), bean.getRefillId(), OrgConstants.card, bean.getAncestors());
    }
}
